/**
 * 奖品管理初始化
 */
var WzPrize = {
    id: "WzPrizeTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
WzPrize.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '奖品id', field: 'prizeId', visible: true, align: 'center', valign: 'middle'},
            {title: '主标题', field: 'title', visible: true, align: 'center', valign: 'middle'},
            {title: '副标题', field: 'subtitle', visible: true, align: 'center', valign: 'middle'},
            {title: '类型', field: 'type', visible: true, align: 'center', valign: 'middle'},
            {title: '子类型', field: 'subType', visible: true, align: 'center', valign: 'middle'},
            // {title: '奖品详情', field: 'details', visible: true, align: 'center', valign: 'middle'},
//            {title: '抽奖消耗', field: 'payAmount', visible: true, align: 'center', valign: 'middle'},
            {title: '积分抵扣', field: 'exchangeAmount', visible: true, align: 'center', valign: 'middle'},
            {title: '总数量', field: 'quantity', visible: true, align: 'center', valign: 'middle'},
            {title: '封面图', field: 'coverUrl', visible: true, align: 'center', valign: 'middle',
                formatter: function (value, row, index) {
                    if (value != undefined && value != '') {
                            value = '<img src="' + value + '" width="45" height="45" style="margin:-8px auto;"/>';
                        }
                    return value;
                }
            },
            {title: '剩余库存', field: 'stock', visible: true, align: 'center', valign: 'middle'},
//            {title: '标签', field: 'label', visible: true, align: 'center', valign: 'middle'},
//            {title: '中奖概率', field: 'ratio', visible: true, align: 'center', valign: 'middle'},
            {title: '状态', field: 'status', visible: true, align: 'center', valign: 'middle'},
            {title: '创建时间', field: 'createTime', visible: true, align: 'center', valign: 'middle'},
            {title: '修改人', field: 'modifierName', visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
WzPrize.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        WzPrize.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加奖品
 */
WzPrize.openAddWzPrize = function () {
    var index = layer.open({
        type: 2,
        title: '添加奖品',
        area: ['800px', '650px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/wzPrize/wzPrize_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看奖品详情
 */
WzPrize.openWzPrizeDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '奖品详情',
            area: ['800px', '650px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/wzPrize/wzPrize_update/' + WzPrize.seItem.prizeId
        });
        this.layerIndex = index;
    }
};

/**
 * 删除奖品
 */
WzPrize.delete = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/wzPrize/delete", function (data) {
                if(data.code == 100){
                    Feng.error(data.message);
                }else if(data.code == 200){
                    Feng.success("删除成功!");
                    WzPrize.table.refresh();
                }
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("wzPrizeId", WzPrize.seItem.prizeId);
            ajax.start();
        };
        Feng.confirm("是否刪除该奖品?", operation);
    }
};

/**
 * 发布奖品
 */
WzPrize.up = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/wzPrize/up", function (data) {
                if(data.code == 100){
                    Feng.error(data.message);
                }else if(data.code == 200){
                    Feng.success("发布成功!");
                    WzPrize.table.refresh();
                }
            }, function (data) {
                Feng.error("发布失败!" + data.responseJSON.message + "!");
            });
            ajax.set("wzPrizeId", WzPrize.seItem.prizeId);
            ajax.start();
        };
        Feng.confirm("是否发布该奖品?", operation);
    }
};

/**
 * 查询奖品列表
 */
WzPrize.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    WzPrize.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = WzPrize.initColumn();
    var table = new BSTable(WzPrize.id, "/wzPrize/list", defaultColunms);
    table.setPaginationType("server");
    WzPrize.table = table.init();
});
